//
//  KKLoginController.m
//  DoMi
//
//  Created by 康凯 on 16/10/26.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKLoginController.h"
#import "KKLoginHttp.h"
#import "KKTabBarController.h"


@interface KKLoginController ()
@property (weak, nonatomic) IBOutlet UITextField *mPhone;

@property (weak, nonatomic) IBOutlet UITextField *mCode;

@property (weak, nonatomic) IBOutlet UIButton *mCodeBtn;

@property (strong,nonatomic) NSTimer *timer;

@property (assign,nonatomic) int count;

@property (assign,nonatomic) int code;
@end

@implementation KKLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    //电量颜色设置
    [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
    // Do any additional setup after loading the view from its nib.
    
}
#pragma mark 获取验证码
- (IBAction)getCode:(id)sender {
    NSString *phone= self.mPhone.text;
    if([self isEmptyString:phone]){
        [self toast:@"请输入手机号码"];
        return;
    }
    self.count=60;
    [self.mCodeBtn setEnabled:NO];
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(scheduleTimer) userInfo:nil repeats:YES];
    
    //发送验证码
    NSDictionary *params=@{@"phone":phone};
    [KKLoginHttp getCode:params success:^(id responseObject) {
        [self toast:@"发送成功"];
    } failure:^(NSError *error) {
        
    }];
    
}
#pragma mark 登录
- (IBAction)login:(id)sender {
    NSString *phone= self.mPhone.text;
    NSString *code=self.mCode.text;
    if([self isEmptyString:phone]){
        [self toast:@"请输入手机号码"];
        return;
    }
    if([self isEmptyString:code]){
        [self toast:@"请输入验证码"];
        return;
    }
    
    //登录
    NSDictionary *params=@{@"phone":phone,@"code":code};
    
    MBProgressHUD *tip=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    tip.label.text=LoadTitle;
    [KKLoginHttp login:params success:^(id responseObject) {
        NSString *uid=[responseObject objectForKey:@"uid"];
        NSString *token=[responseObject objectForKey:@"token"];
        
        NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
        [user setObject:uid forKey:@"uid"];
        [user setObject:token forKey:@"token"];
        [user synchronize];
        [tip hideAnimated:YES];
        KKTabBarController *bar=[[KKTabBarController alloc] init];
        appWindow.rootViewController=bar;
        
    } failure:^(NSError *error) {
        [tip hideAnimated:YES];
    }];
    
}

#pragma mark 倒计时
-(void)scheduleTimer{
    self.count--;
    NSString *time=[NSString stringWithFormat:@"%ds重新获取",self.count];
    [self.mCodeBtn setTitle:time forState:UIControlStateNormal];
    if(self.count==0){
        [self.timer invalidate];
        [self.mCodeBtn setEnabled:YES];
        [self.mCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.count=60;
    }
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.mPhone resignFirstResponder];
    [self.mCode resignFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.timer invalidate];
}


@end
